import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta: {
            title: "首页",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: true//是否缓存
        },
    },
    {
        path: '/login',
        name: 'LoginAndRegister',
        component: () => import("../views/Login.vue"),
        meta: {
            title: "登录",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: true//是否缓存
        },
    },
    {
        path: '/allPro',
        name: 'allPro',
        component: () => import("../views/AllProductList"),
        meta: {
            title: "全部商品",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
    },
    {
        path: '/UserInfo',
        // name: 'userInfo',
        component: () => import("../views/userInfo/UserInfo"),
        meta: {
            title: "用户信息",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
        children: [
            {
                path: "/UserInfo",
                redirect: '/UserInfo/Integration',
            },
            {
                path: 'Integration',
                component: () => import("../views/userInfo/componets/Integration"),
                meta: {
                    title: "积分明细",
                    headerShow: true, //是否显示头底部
                    footerShow: true,
                    iSkeepAlive: false//是否缓存
                },
            },
            {
                path: 'balance',
                name: 'Balance',
                component: () => import("../views/userInfo/componets/balance"),
                meta: {
                    title: "我的余额",
                    headerShow: true, //是否显示头底部
                    footerShow: true,
                    iSkeepAlive: false//是否缓存
                },
            },
            {
                path: 'address',
                name: 'Address',
                component: () => import("../views/userInfo/componets/address"),
                meta: {
                    title: "我的地址",
                    headerShow: true, //是否显示头底部
                    footerShow: true,
                    iSkeepAlive: false//是否缓存
                },
            },
            {
                path: 'order',
                name: 'Address',
                component: () => import("../views/userInfo/componets/order"),
                meta: {
                    title: "我的订单",
                    headerShow: true, //是否显示头底部
                    footerShow: true,
                    iSkeepAlive: false//是否缓存
                },
            },
            {
                path: 'coupon',
                name: 'Coupon',
                component: () => import("../views/userInfo/componets/coupon"),
                meta: {
                    title: "我的优惠",
                    headerShow: true, //是否显示头底部
                    footerShow: true,
                    iSkeepAlive: false//是否缓存
                },
            },
        ]
    },
    {
        path: '/prductDetail',
        name: 'prductDetail',
        component: () => import("../views/productDetail/ProductDetail"),
        meta: {
            title: "我的商品详情",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
    },
    {
        path: '/successAddCart',
        name: 'successAddCart',
        component: () => import("../views/cart/successAddCard"),
        meta: {
            title: "成功添加购物车",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
    },
    {
        path: '/myCart',
        name: 'myCart',
        component: () => import("../views/cart/Cart"),
        meta: {
            title: "我的购物车",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
        
    },
    {
        path: '/submitOrder',
        name: 'submitOrder',
        component: () => import("../views/order/submitOrder"),
        meta: {
            title: "订单确认",
            headerShow: true, //是否显示头底部
            footerShow: true,
            iSkeepAlive: false//是否缓存
        },
    },
    {
        path: '/payPrice',
        name: 'PayPrice',
        component: () => import("../views/PayPrice/PayPrice"),
        meta: {
            title: "订单支付",
            headerShow: true, //是否显示头底部
            footerShow: false,
            iSkeepAlive: false//是否缓存
        },
    },
    {
        path: '/paySuccess',
        name: 'paySuccess',
        component: () => import("../views/paySuccess/PaySuccess"),
        meta: {
            title: "支付成功",
            headerShow: true, //是否显示头底部
            footerShow: false,
            iSkeepAlive: false//是否缓存
        },
    },

]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })
const router = new VueRouter({
    mode: 'hash',
    routes,
    base: process.env.BASE_URL,
    scrollBehavior(to, from, savedPosition) {
        //x  y两个值
        return {
            y: 0
        }
    }
})


router.beforeEach((to, from, next) => {
    // 根据路由元信息设置文档标题
    window.document.title = to.meta.title || ""
    if (localStorage.getItem("TOKEN")) {
        if (to.path == "/login") {
            next("/")
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
